<?php

namespace App\Admin\Actions\Category;

use App\Library\Logic\OperateLogLogic;
use App\Models\ShopCategoryModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class CategoryEditAction extends RowAction
{
    public $name = '编辑';

    public function handle(ShopCategoryModel $model, Request $request): Response
    {
        if (is_null($model) || $model->status_delete) return $this->response()->error(__('errorCode.433'))->refresh();
        $info = ShopCategoryModel::query()->where([
            'category_parent_id' => $request->categoryParentId,
            'category_name' => $request->categoryName,
            'status_delete' => 0
        ])->where('category_id', '!=', $model->category_id)->first();
        if (!is_null($info)) return $this->response()->error('分类名称不能重复')->refresh();
        $model->category_name = $request->categoryName;
        $model->category_status = $request->status;
        $model->sort = $request->sort;
        $model->class_img_url = $request->categoryImg;
        $model->class_jump_url = $request->categoryJump;
        $categoryLevel = explode(',', $model->category_level);
        $thirdLevelInfo = [];
        if (count($categoryLevel) === 1 && $model->category_parent_id > 0) {
            $model->category_parent_id = $request->categoryParentId;
            $model->category_top_parent_id = $request->categoryParentId;
            $model->category_level = $request->categoryParentId;
        } else if (count($categoryLevel) > 1) {
            $model->category_parent_id = $request->categoryParentId;
            $topCategory = ShopCategoryModel::findByWhere(['categoryId' => $request->categoryParentId]);
            $model->category_parent_id = $request->categoryParentId;
            $model->category_top_parent_id = $topCategory->category_id;
            $model->category_level = $topCategory->category_id . ',' . $request->categoryParentId;
            $thirdLevelInfo = [
                'categoryTopParentId' => $topCategory->category_id,
                'categoryLevel' => $topCategory->category_id . ',' . $request->categoryParentId
            ];
        }

        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.category.edit', empty($thirdLevelInfo) ?? $thirdLevelInfo);
        return $this->response()->success('操作成功')->refresh();
    }

    public function form(ShopCategoryModel $model): void
    {
        if ($model->category_parent_id !== 0) {
            $categoryLLevel = explode(',', $model->category_level);
            if (count($categoryLLevel) === 1 && $model->category_parent_id > 0) {
                $firstCategory = ShopCategoryModel::query()->where([
                    'category_parent_id' => 0,
                    'status_delete' => 0
                ])->pluck('category_name', 'category_id');
                if (!is_null($firstCategory)) $this->select('categoryParentId', __('category.categoryParentId'))->options($firstCategory);
                else $this->select('categoryParentId', __('category.categoryParentId'))->disable()->options([null => '无可用一级分类']);
            } else if (count($categoryLLevel) >= 1) {
                $firstCategory = ShopCategoryModel::query()->where([
                    'category_parent_id' => 0,
                    'status_delete' => 0
                ])->pluck('category_id')->toArray();
                if (!empty($firstCategory)) $this->select('categoryParentId', __('category.categoryParentId'))->options(ShopCategoryModel::query()
                    ->where('status_delete', 0)->whereIn('category_parent_id', $firstCategory)->pluck('category_name', 'category_id'));
                else $this->select('categoryParentId', __('category.categoryParentId'))->disable()->options([null => '无可用二级分类']);
            }

        }

        $this->text('categoryName', __('category.category_name'))->default($model->category_name)->rules('required|string|max:255');
        $this->select('status', __('category.status'))->default($model->category_status)->options(__('category.statusArr'));
        $this->text('sort', __('category.sort'))->default($model->sort);
        $this->text('categoryImg', '分类图片路径：')->default($model->class_img_url);
        $this->text('categoryJump', '分类跳转地址（如外链请使用http或https开头）：')->default($model->class_jump_url);

    }
}
